'''
Author: caishuyang
Date: 2023-02-20 15:48:52
LastEditors: caishuyang
LastEditTime: 2023-03-06 09:44:07
Description: 
'''
from _03Player import Player2D,Player3D
from _04serial import mySerial
from _05Publisher import Publisher
import threading
import time
from queue import Queue
from keyboard import *

def read_serial(serial,q):
    #serial.open_ser()
    serial.imitate_read(q)
    #qserial.close_ser()

def pub_xyz(publish,recieve_q,pub_qlist):
    publish.pub(recieve_q,pub_qlist)

def play_video(player,q):
    player.playvideo(q)

def quit(mylist):   
    for mine in mylist:
        mine.flag=False
        time.sleep(1e-2)

if __name__=="__main__":
    q_serial=Queue(maxsize=1)
    q1=Queue(maxsize=1)
    q2=Queue(maxsize=1)
    qlist=[q1,q2]

    myser=mySerial()
    publisher=Publisher(8,2)
    player2d=Player2D()
    player3d=Player3D()
    mylist=[player3d,player2d,publisher,myser]

    read_ser=threading.Thread(target=read_serial,args=(myser,q_serial))
    pub_thread=threading.Thread(target=pub_xyz,args=(publisher,q_serial,qlist))
    play2d=threading.Thread(target=play_video,args=(player2d,q1))
    play3d=threading.Thread(target=play_video,args=(player3d,q2))

    read_ser.start()
    pub_thread.start()
    play2d.start()
    play3d.start()

    add_hotkey('q',quit,args=(mylist,))
    record('q')